package nickthissen.iracing.activities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import java.sql.DriverManager;
import java.util.ArrayList;

import nickthissen.iracing.adapters.DriverListAdapter;
import nickthissen.iracing.common.Error;
import nickthissen.iracing.common.DataLoader;
import nickthissen.iracing.common.Task;
import nickthissen.iracing.datamodels.Driver;
import nickthissen.iracing.datamodels.Result;
import nickthissen.iracing.iracingforum2.R;
import nickthissen.iracing.networking.Networking;
import nickthissen.iracing.parsing.DriverListParser;

public class DriverListActivity extends TaskItemListActivity<Driver>
{	
	private ArrayList<Driver> drivers;

    private String storedName;

	private ImageButton btnFindUser;
	private EditText txtUser;
	private ProgressDialog dialog;
	
	public static final int DRIVER_CHOSEN = 1;
    private static final String NAME = "NAME";
		
	@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.driverlist);
        
        btnFindUser = (ImageButton) this.findViewById(R.id.btnFindUser);
        txtUser = (EditText) this.findViewById(R.id.txtUser);
        
        btnFindUser.setOnClickListener(new View.OnClickListener() {
						@Override
			public void onClick(View v) {
				find();
			}
		});

        this.getSupportActionBar().setTitle("Search for members");
    }

    @Override
    protected Task<?> createTask()
    {
        return new Task<String>(this, loader);
    }

    @Override
    protected void saveTaskInput(Bundle state)
    {
        state.putString(NAME, storedName);
    }

    @Override
    protected Object restoreTaskInput(Bundle state)
    {
        return state.getString(NAME);
    }

    private void find()
	{
		String username = txtUser.getText().toString();
		if (username != null && !username.equals(""))
		{
			this.loadDrivers(username);
		}
		else
		{
			Toast.makeText(this, "Please submit a username to search for...", Toast.LENGTH_LONG).show();
		}
	}

	private void loadDrivers(String username)
	{
        storedName = username;
		this.startLoading("Searching for matching members...", username, false);
	}

    @Override
    protected void loadData()
    {
        this.loadDrivers(storedName);
    }

    @Override
	protected void setItems(ArrayList<Driver> items) 
	{
		DriverListAdapter adapter = new DriverListAdapter(this, R.layout.driverrow, items);
		this.setListAdapter(adapter);
	}

	@Override
	public void onItemClicked(Driver item) 
	{
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		bundle.putSerializable("DRIVER", item);
		intent.putExtras(bundle);
		this.setResult(DRIVER_CHOSEN, intent);
		this.finish();
	}

    private DataLoader<String> loader = new DataLoader<String>()
    {
        @Override
        public Result getData(String in)
        {
            Result result = Networking.getDrivers(this.getContext(), in);
            if (result.Success)
            {
                String json = (String)result.Object;
                return new DriverListParser().parse(json);
            }
            else
            {
                return result;
            }
        }

        @Override
        public void onFinished(Result out)
        {
            if (out.Success)
            {
                drivers = (ArrayList<Driver>)out.Object;
            }
            else
            {
                Error.fromResult(out).report(this.getContext());
            }

            setItems(drivers);
            finishedLoading();
        }
    };
}
